package com.woniu.zsd.recycle.controller;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.zsd.common.util.JWTUtil;
import com.woniu.zsd.common.util.Result;
import com.woniu.zsd.recycle.dto.*;
import com.woniu.zsd.recycle.form.*;
import com.woniu.zsd.recycle.model.RecyclingOrder;
import com.woniu.zsd.recycle.param.*;
import com.woniu.zsd.recycle.service.RecyclingOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 彭于晏
 * @since 2025年02月18日
 */
@RestController
@RequestMapping("/recyclingOrder")
@Api(tags = "回收订单管理")
public class RecyclingOrderController {
    @Resource
    private RecyclingOrderService recyclingOrderService;
    @Resource
    private JWTUtil jwtUtil;
    @PostMapping("/addRecyclingOrder")
    @ApiOperation(value = "新增回收订单")
    public Result addRecyclingOrder(@RequestBody AddRecyclingOrderForm form, @RequestHeader("token") String token)throws Exception{
        Integer userId = Integer.parseInt(jwtUtil.parseToken(token).get("userId").toString());
        System.out.println("token解析出来的userId："+userId);
        AddRecyclingOrderParam param = BeanUtil.toBean(form, AddRecyclingOrderParam.class);
        param.setUserId(userId);
        recyclingOrderService.addRecyclingOrder(param);
        return Result.success("新增回收订单成功",null);
    }
    @PostMapping("/receivingOrders")
    @ApiOperation(value = "站点接单")
    public Result receivingOrders(@RequestBody ReceivingOrdersForm form) throws Exception {
        ReceivingOrdersParam param = BeanUtil.toBean(form, ReceivingOrdersParam.class);
        recyclingOrderService.receivingOrders(param);
        return Result.success("站点接单成功",null);
    }
    @PostMapping("/confirmVisit")
    @ApiOperation(value = "回收人员确认上门服务")
    public Result confirmVisit(@RequestBody ConfirmVisitForm form)throws Exception{
        recyclingOrderService.confirmVisit(BeanUtil.toBean(form,ConfirmVisitParam.class));
        return Result.success("回收人员确认上门服务",null);
    }
    @PostMapping("/cancelRecyclingOrder")
    @ApiOperation(value = "取消回收订单")
    public Result cancelRecyclingOrder(String recyclingOrderNum) throws Exception {
        recyclingOrderService.cancelRecyclingOrder(recyclingOrderNum);
        return Result.success("取消回收订单成功",null);
    }
    @PostMapping("/confirmOrder")
    @ApiOperation(value = "用户确认订单")
    public Result confirmOrder(@RequestBody ConfirmOrderForm form) throws Exception {
        recyclingOrderService.confirmOrder(BeanUtil.toBean(form, ConfirmOrderParam.class));
        return Result.success("用户确认订单成功",null);
    }
    @PostMapping("/selectOrderById")
    @ApiOperation(value = "站点查询待接订单")
    public Result selectOrderById(Integer stationId)throws Exception{
        List<SelectOrderByIdDto> dto = recyclingOrderService.selectOrderById(stationId);
        return Result.success("站点查询待接订单成功",dto);
    }
    @PostMapping("/selectAllOrderByUserId")
    @ApiOperation(value = "用户查询订单")
    public Result selectAllOrderByUserId(Integer userId)throws Exception{
        List<RecyclingOrder> dtos = recyclingOrderService.selectAllOrderByUserId(userId);
        return Result.success("用户查询订单成功",dtos);
    }
    @PostMapping("/selectCancelOrderByUserId")
    @ApiOperation(value = "用户查询取消订单")
    public Result selectCancelOrderByUserIdAndStatus(Integer userId, Integer status)throws Exception{
        List<SelectCancelOrderByUserIdDro> dtos = recyclingOrderService.selectCancelOrderByUserIdAndStatus(userId,status);
        return Result.success("用户查询取消订单成功",dtos);
    }
    @PostMapping("/selectOrderByCondition")
    @ApiOperation(value = "根据条件查询订单")
    public Result selectOrderByCondition(@RequestBody ConditionForm form)throws Exception{
        PageInfo<SelectOrderByConditionDto> dtos=recyclingOrderService.selectOrderByCondition(BeanUtil.toBean(form, ConditionParam.class));
        return Result.success("根据条件查询订单成功",dtos);
    }
    @PostMapping("/stationSelectOrderByCondition")
    @ApiOperation(value = "站点条件查询本站点订单")
    public Result stationSelectOrderByCondition(@RequestBody StationConditionForm form)throws Exception{
        PageInfo<SelectOrderByConditionDto> dtos=recyclingOrderService.stationSelectOrderByCondition(BeanUtil.toBean(form, StationConditionParam.class));
        return Result.success("站点条件查询本站点订单成功",dtos);
    }

    @GetMapping("/selectAllOrderByStation")
    @ApiOperation(value = "查询所有订单根据站点分类")
    public Result selectAllOrderByStation()throws Exception{
        List<OrderDtoByStation> dtos = recyclingOrderService.selectAllOrderByStation();
        return Result.success("根据站点分类查询订单成功",dtos);
    }

    @GetMapping("/selectAllOrderByType")
    @ApiOperation(value = "查询所有订单根据类型分类")
    public Result selectAllOrderByType()throws Exception{
        List<selectAllOrderByType> dtos = recyclingOrderService.selectAllOrderByType();
        return Result.success("根据类型分类查询订单成功",dtos);
    }

}

